//Powered By SirYuxuan team MakeCode 
//www.yuxuan66.com
package com.systemsetup.systemsetup.web.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.systemsetup.systemsetup.common.base.BaseService;
import com.systemsetup.systemsetup.model.Department;
import com.systemsetup.systemsetup.model.User;
import com.systemsetup.systemsetup.support.model.RespEntity;
import com.systemsetup.systemsetup.support.mybatisplus.QueryWrapper;
import com.systemsetup.systemsetup.support.utils.requestutil.HttpRequestUtil;
import com.systemsetup.systemsetup.web.mapper.DepartmentMapper;
import com.systemsetup.systemsetup.web.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class DepartmentService extends BaseService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private DepartmentMapper departmentMapper;

    private static final Logger logger = LoggerFactory.getLogger(DepartmentService.class);

    /**
     * 部门树形菜单
     *
     * @param jsonParam
     * @return
     */
    public RespEntity departmentTree(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        String userId = token.getString("user_id");
        String userType = token.getString("user_type");
        if(userType.equals("1")){

        }else if(userType.equals("2")){
            jsonParam.put("businessId",homeId);
        }
        List<Department> list = departmentMapper.selectList(new QueryWrapper<Department>().eq("business_id",jsonParam.getStr("businessId")));
        Department department = new Department();
        department.setBusinessId(jsonParam.getStr("businessId"));
        department.setId("0");
        department.setName("总部");
        department.put("list",list);
        return RespEntity.success(department);
    }

    /**
     * 部门添加
     *
     * @param jsonParam
     * @return
     */
    public RespEntity departmentAdd(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        Department department = new Department();
        department.setBusinessId(homeId);
        department.setId(UUID.randomUUID().toString());
        department.setDes(jsonParam.getStr("des"));
        department.setName(jsonParam.getStr("name"));
        departmentMapper.insert(department);
        return RespEntity.success();
    }

    /**
     * 部门修改
     *
     * @param jsonParam
     * @return
     */
    public RespEntity departmentUpdate(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        String homeId = token.getString("home_id");
        Department department = new Department();
        department.setBusinessId(homeId);
        department.setId(jsonParam.getStr("id"));
        department.setDes(jsonParam.getStr("des"));
        department.setName(jsonParam.getStr("name"));
        departmentMapper.updateById(department);
        return RespEntity.success();
    }

    /**
     * 部门删除
     *
     * @param jsonParam
     * @return
     */
    public RespEntity departmentDelete(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));

        int count = userMapper.selectCount(new QueryWrapper<User>().eq("department_id", jsonParam.getStr("id")));
        if (count == 0) {
            departmentMapper.deleteById(jsonParam.getStr("id"));
        }else{
            return RespEntity.fail("部门下有账号，不能删除");
        }
        return RespEntity.success();
    }

    /**
     * 部门详情
     *
     * @param jsonParam
     * @return
     */
    public RespEntity departmentDetail(JSONObject jsonParam) {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        com.alibaba.fastjson.JSONObject token = makeDataForToken(jsonParam, "systemsetup", "config", "getDecodeToken", "systemsetup");
        if (StrUtil.isNotBlank(token.getString("methodCodeFlag"))) return RespEntity.expire();
        if (StrUtil.isNotBlank(token.getString("methodErrorFlag"))) return RespEntity.fail(token.getString("msg"));
        Department department = departmentMapper.selectById(jsonParam.getStr("id"));
        return RespEntity.success(department);
    }
}
